<template>
  <div class="dashboard">
    <!-- 标题栏 -->
    <div class="header">
      <div class="header-left">
        <div class="decoration-line"></div>
      </div>
      <h1>电商平台实时监控系统</h1>
      <div class="header-right">
        <!-- 主题切换按钮已去除 -->
        <div class="decoration-line"></div>
        <div class="time">
          <svg class="icon" viewBox="0 0 1024 1024" width="18" height="18">
            <path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64z m0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z" fill="#ffffff"/>
            <path d="M686.7 638.6L544.1 535.5V288c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8v275.4c0 2.6 1.2 5 3.3 6.5l165.4 120.6c3.6 2.6 8.6 1.8 11.2-1.7l28.6-39c2.6-3.7 1.8-8.7-1.9-11.2z" fill="#ffffff"/>
          </svg>
          <span>{{ currentTime }}</span>
        </div>
      </div>
    </div>

    <!-- 分类切换功能已移除 -->

    <!-- 数据可视化网格 -->
    <div class="grid-container">
      <TrendChartBox :is-dark-theme="isDarkTheme" />
      <MapChartBox :is-dark-theme="isDarkTheme" />
      <PieChartBox :is-dark-theme="isDarkTheme" />
      <SalesChartBox :is-dark-theme="isDarkTheme" />
      <RankingChartBox :is-dark-theme="isDarkTheme" />
      <GaugeChartBox :is-dark-theme="isDarkTheme" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, onUnmounted } from 'vue';
import { nextTick } from 'vue';
import TrendChartBox from '@/Component/TrendChartBox.vue';
import MapChartBox from '@/Component/MapChartBox.vue';
import PieChartBox from '@/Component/PieChartBox.vue';
import SalesChartBox from '@/Component/SalesChartBox.vue';
import RankingChartBox from '@/Component/RankingChartBox.vue';
import GaugeChartBox from '@/Component/GaugeChartBox.vue';

const currentTime = ref('2019-01-01 00:00:00');
const isDarkTheme = ref(true);
let timer: number;

// 等比例缩放函数
function handleResize() {
  const dashboard = document.querySelector('.dashboard') as HTMLElement;
  if (!dashboard) return;
  
  const scaleX = window.innerWidth / 1920;
  const scaleY = window.innerHeight / 1080;
  const scale = Math.min(scaleX, scaleY);
  
  dashboard.style.transform = `scale(${scale})`;
}

// 全局 ESC 退出放大（全屏）逻辑
function handleEscKey(e: KeyboardEvent) {
  if (e.key === 'Escape') {
    // 触发所有模块的退出放大事件
    const fullscreenEls = document.querySelectorAll('.chart-box.fullscreen');
    fullscreenEls.forEach((el) => {
      // 触发自定义事件，模块内监听此事件实现退出放大
      el.dispatchEvent(new CustomEvent('exit-fullscreen'));
    });
  }
}

// 主题切换功能
function toggleTheme() {
  isDarkTheme.value = !isDarkTheme.value;
}

// 更新时间
function updateTime() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const hours = String(now.getHours()).padStart(2, '0');
  const minutes = String(now.getMinutes()).padStart(2, '0');
  const seconds = String(now.getSeconds()).padStart(2, '0');
  currentTime.value = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

onMounted(() => {
  updateTime();
  timer = window.setInterval(updateTime, 1000);
  window.addEventListener('keydown', handleEscKey);
  handleResize();
  window.addEventListener('resize', handleResize);
});

onUnmounted(() => {
  clearInterval(timer);
  window.removeEventListener('keydown', handleEscKey);
  window.removeEventListener('resize', handleResize);
});
</script>

<style>
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden !important;
  background: #0e1621;
}

#app {
  height: 100%;
  overflow: hidden !important;
  background: #0e1621;
}
</style>

<style scoped>
.dashboard {
  width: 1920px;
  height: 1080px;
  background: radial-gradient(circle at top, rgba(20, 42, 71, 0.88), #0e1621 58%);
  padding: 5px;
  box-sizing: border-box;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 0;
  min-height: 0;
  transform-origin: left top;
  position: absolute;
  left: 0;
  top: 0;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  flex-shrink: 0;
  width: 100%;
  box-sizing: border-box;
}

.header-left,
.header-right {
  display: flex;
  align-items: center;
  gap: 15px;
  flex: 1;
}

.header-right {
  justify-content: flex-end;
}

.theme-toggle {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ffffff;
}

.theme-toggle:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(0, 150, 255, 0.5);
  transform: scale(1.05);
}

.theme-toggle svg {
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3));
}

.decoration-line {
  width: 120px;
  height: 2px;
  background: linear-gradient(to right, transparent, rgba(0, 150, 255, 0.65), transparent);
}

.header h1 {
  color: #ffffff;
  font-size: 28px;
  font-weight: 600;
  margin: 0;
  letter-spacing: 3px;
  text-align: center;
  white-space: nowrap;
}

.category-bar {
  display: flex;
  gap: 10px;
  padding: 0 12px 4px;
  flex-wrap: wrap;
  justify-content: center;
  width: calc(100% - 10px);
  margin: 0 0 2px 0;
}

.category-button {
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(0, 150, 255, 0.4);
  border-radius: 999px;
  padding: 4px 14px;
  color: #c1ccdd;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.category-button:hover {
  color: #ffffff;
  border-color: rgba(0, 150, 255, 0.7);
  box-shadow: 0 0 8px rgba(0, 150, 255, 0.35);
}

.category-button.active {
  color: #ffffff;
  background: rgba(0, 150, 255, 0.35);
  border-color: rgba(0, 150, 255, 0.85);
  box-shadow: 0 0 10px rgba(0, 150, 255, 0.45);
}

.time {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #ffffff;
  font-size: 16px;
  font-family: 'Segoe UI', 'Arial', sans-serif;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
  margin-left: auto;
  margin-right: 0;
}

.time .icon {
  flex-shrink: 0;
}

.grid-container {
  display: grid;
  grid-template-columns: 1.3fr 2.7fr 1.2fr;
  grid-template-rows: 1.05fr 0.85fr;
  grid-template-areas:
    "trend map pie"
    "sales ranking gauge";
  gap: 30px;
  align-items: stretch;
  flex: 1;
  overflow: hidden;
  min-height: 0;
  width: 100%;
  max-width: none;
  margin: 0;
  grid-auto-rows: minmax(0, 1fr);
}


:deep(.trend-box) {
  grid-area: trend;
}

:deep(.map-box) {
  grid-area: map;
  min-height: 0;
}

:deep(.pie-box) {
  grid-area: pie;
}

:deep(.sales-box) {
  grid-area: sales;
}

:deep(.ranking-box) {
  grid-area: ranking;
  min-height: 0;
}

:deep(.gauge-box) {
  grid-area: gauge;
  min-height: 0;
}

:deep(.map-chart) {
  height: 100% !important;
}


:deep(.chart-box) {
  background: linear-gradient(135deg, rgba(14, 28, 44, 0.92), rgba(9, 19, 33, 0.88));
  border: 1px solid rgba(48, 65, 86, 0.65);
  border-radius: 22px;
  padding: 0;
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.38);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

:deep(.chart-box.fullscreen) {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  width: 100vw;
  height: 100vh;
  border-radius: 0;
  grid-column: unset;
}

:deep(.chart-box.fullscreen .chart) {
  height: calc(100vh - 60px) !important;
}

:deep(.chart-box.fullscreen .map-chart) {
  height: calc(100vh - 60px) !important;
}

:deep(.chart-box:hover) {
  border-color: rgba(0, 150, 255, 0.55);
  box-shadow: 0 10px 24px rgba(0, 100, 200, 0.22);
}

:deep(.chart-header) {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid rgba(48, 65, 86, 0.5);
  flex-shrink: 0;
}

:deep(.chart-box h3) {
  color: #ffffff;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.5px;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

:deep(.title-icon) {
  color: #0096ff;
  font-size: 20px;
  font-weight: bold;
  line-height: 1;
}

:deep(.dropdown-icon) {
  margin-left: 5px;
  cursor: pointer;
  opacity: 0.6;
  transition: opacity 0.2s;
}

:deep(.dropdown-icon:hover) {
  opacity: 1;
}

:deep(.fullscreen-btn) {
  background: none;
  border: none;
  color: #8492a6;
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
  transition: color 0.2s;
  opacity: 0.7;
}

:deep(.fullscreen-btn:hover) {
  color: #0096ff;
  opacity: 1;
}

:deep(.chart) {
  width: 100%;
  height: 100%;
  flex: 1;
  padding: 10px 12px;
  box-sizing: border-box;
  min-height: 0;
}

:deep(.pie-label) {
  position: absolute;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  color: #ffffff;
  font-size: 16px;
  font-weight: 400;
  pointer-events: none;
}
</style>